
package com.nl.controller.web.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.interceptor.SysContext;
import com.nl.controller.common.mapper.CommonMapper;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.SmsContent;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.mapper.SmsContentMapper;
import com.nl.controller.web.mapper.SmsTemplateCheckMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class SmsContentService {
    @Autowired
    private SmsContentMapper smsContentMapper;

    @Autowired
    private CommonService commonService;
    
    @Autowired
	private CommonMapper commonMapper;

    
    @Autowired
	private SmsTemplateCheckMapper smsTemplateCheckMapper;
    
    
    @SystemServiceLog(description = "短信内容管理", value = false)
    public PageInfo<SmsContent> findContent(int page, int limit, String cityId, String mgrId, String mgrName) {
    	
        Map<String, Object> map = new HashMap<>();
        map.put("cityId", cityId);
        map.put("mgrId", mgrId);
        map.put("mgrName", mgrName);
        PageHelper.startPage(page, limit);
        PageInfo<SmsContent> pageInfo;
        try {
            List<SmsContent> list = smsContentMapper.findContent(map);
            //判断现在发的信息的内容
           for(SmsContent content:list){
                String effectTime = content.getEffectTime();
                if (effectTime != null && !"".equals(effectTime)) {
                    log.info(content.getEffectTime());
                    try {
                        if (this.judgement(content)) {
                            content.setContent(content.getNewContent());
                            content.setNewContent("");
                            content.setCreateTime(content.getEffectTime());
                            content.setEffectTime("");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            pageInfo = new PageInfo<>(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("短信项目内容查询：" + cityId);
            throw new SmsException("短信项目内容查询：" + cityId);
        }
        return pageInfo;
    }

    //@SystemServiceLog(description = "查询单个模板内容", value = false)
    public SmsContent findContentByMgrId(String mgrId) {
        SmsContent smsContent = null;
        try {
            smsContent = smsContentMapper.findContentById(mgrId);
            /*if(smsContent==null){
                return smsContent;
            }else{
                if (smsContent.getNewContent() != null&&!"".equals(smsContent.getNewContent())) {
                    //判断是否生效
                    if (this.judgement(smsContent)) {
                        smsContent.setContent(smsContent.getNewContent());
                    }
                }else{
                    smsContent.setContent(smsContent.getContent());
                }
            }*/
        } catch (Exception e) {
            e.printStackTrace();
            log.error("短信项目内容查询：" + mgrId);
            throw new SmsException("短信项目内容查询：" + mgrId);
        }
        return smsContent;
    }


    /**
     * 判断生效时间时和现在时间的关系
     * true 生效
     * false 未生效
     *
     * @param smsContent
     * @return
     */
    private boolean judgement(SmsContent smsContent) throws Exception {
        String effectTime = smsContent.getEffectTime();
        if (effectTime != null&&!"".equals(effectTime)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            date = sdf.parse(effectTime);
            Date sysdate = new Date();
            if (date.getTime() < sysdate.getTime()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

}
